import numpy as np

# 将字词word转为索引矩阵
# param w2vModel
# param sentences
# param max_sentence_length
def word2indexMat(w2vModel, sentences, max_sentence_length):
    nums_sample = len(sentences)
    indexMat = np.zeros((nums_sample, max_sentence_length), dtype='int')
    rows = 0
    for sentence in sentences:
        indexCounter = 0
        for word in sentence.split(' '):
            try:
                index = w2vModel.wv.vocab[word].index  # 获得单词word的下标
                indexMat[rows][indexCounter] = index
            except:
                indexMat[rows][indexCounter] = 0  # Vector for unkown words
            indexCounter = indexCounter + 1
            if indexCounter >= max_sentence_length:
                break
        rows += 1
    return indexMat